import { computed, reactive, watchEffect } from 'vue';
import { useDependencyInjection } from '../../../utils/useDependencyInjection';

export const useSubCategory = (props, emits) => {
    const subCateData = reactive({
        data: {
            fileList: [],
        },
    });
    watchEffect(() => {
        subCateData.data = props.subCategoryItem;
    });
    /**
     * @description: 判断是否展示小类
     */
    const showEmptySub = computed(() => {
        if (!props.attachmentConfigs.showEmptySubclass && !props.attachmentConfigs.showUpload) {
            if (subCateData.data.fileList.length == 0) {
                return false
            }
        }
        return true
    })

    /**
     * @description: 判断是否可以继续上传
     */
    const showUpload = computed(() => {
        const { uploadOperation, fileMaximum, fileList } = subCateData.data;
        if (uploadOperation == 1 && props.attachmentConfigs.showUpload && fileList.length < fileMaximum) {
            return true;
        }
        return false;
    });
    // 新增文件
    const uploadSuccess = files => {
        subCateData.data.fileList = [...subCateData.data.fileList, ...files];
        emits('uploadSuccess', subCateData.data);
    };
    /**
     * @description: 删除文件
     */
    const deleteFile = file => {
        subCateData.data.fileList.splice(
            subCateData.data.fileList.findIndex(item => item.id == file.id),
            1
        );
        emits('deleteSuccess', subCateData.data);
    };
    return {
        showUpload,
        uploadSuccess,
        subCateData,
        deleteFile,
        showEmptySub,
    };
};